package com.car.loan.project.cm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.enums.SwitchStatus;
import com.car.loan.common.enums.UserStatus;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.DateUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.project.cm.domain.CmDealerAccount;
import com.car.loan.project.cm.domain.CmUserAccount;
import com.car.loan.project.cm.mapper.CmUserAccountMapper;
import com.car.loan.project.cm.service.ICmUserAccountService;
import com.car.loan.project.cm.util.select.CmUserAccountBaseSelect;
import com.car.loan.project.cm.util.select.DealerAccountBaseSelect;
import com.car.loan.project.cm.util.select.UserAccountBaseDataSelect;
import com.car.loan.project.cm.util.vo.CmUserAccountBaseVO;
import com.car.loan.project.cm.util.vo.SelectedAccountVo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务员账号信息Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-19
 */
@Service
public class CmUserAccountServiceImpl implements ICmUserAccountService {

    @Autowired
    private CmUserAccountMapper cmUserAccountMapper;

    /**
     * 查询业务员账号信息
     *
     * @param id 业务员账号信息ID
     * @return 业务员账号信息
     */
    @Override
    public CmUserAccount selectCmUserAccountById(Long id) {
        return cmUserAccountMapper.selectById(id);
    }

    @Override
    public Page<CmUserAccountBaseVO> selectPage(CmUserAccountBaseSelect cmUserAccountBaseSelect) {
        Page<CmUserAccountBaseVO> page = new Page<>(cmUserAccountBaseSelect.getPageNum(),
            cmUserAccountBaseSelect.getPageSize());
        return cmUserAccountMapper.findPage(page, cmUserAccountBaseSelect);
    }

    /**
     * 查询业务员账号信息列表
     *
     * @param cmUserAccount 业务员账号信息
     * @return 业务员账号信息
     */
    @Override
    public List<CmUserAccount> selectCmUserAccountList(CmUserAccount cmUserAccount) {
        QueryWrapper<CmUserAccount> queryWrapper = new QueryWrapper<>(cmUserAccount);
        return cmUserAccountMapper.selectList(queryWrapper);
    }

    @Override
    public List<SelectedAccountVo> selectedPage(UserAccountBaseDataSelect select) {
        CmUserAccount cmUserAccountQuery = new CmUserAccount();
        if (null != select.getUserId() && select.getUserId() > 0) {
            cmUserAccountQuery.setUserId(select.getUserId());
        }
        QueryWrapper<CmUserAccount> queryWrapper = new QueryWrapper<>(cmUserAccountQuery);

        queryWrapper.select("id,account_name,bank_name,bank_account_no,status");
        if (null == select.getOf() || SwitchStatus.OPEN.status == select.getOf()) {
            queryWrapper.eq("status", UserStatus.OK.getCode());
        } else {
            queryWrapper.in("status", UserStatus.OK.getCode(), UserStatus.DISABLE.getCode());
        }
        queryWrapper.orderByDesc("id");
        List<CmUserAccount> cmUserAccounts = cmUserAccountMapper.selectList(queryWrapper);
        List<SelectedAccountVo> selectedVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(cmUserAccounts)) {
            cmUserAccounts.forEach(data -> {
                SelectedAccountVo selectedAccountVo = new SelectedAccountVo();
                BeanUtils.copyBeanProp(selectedAccountVo, data);
                selectedAccountVo.setId(String.valueOf(data.getId()));
                selectedVos.add(selectedAccountVo);
            });
        }
        return selectedVos;
    }

    /**
     * 新增业务员账号信息
     *
     * @param cmUserAccount 业务员账号信息
     * @return 结果
     */
    @Override
    public int insertCmUserAccount(CmUserAccount cmUserAccount) {
        cmUserAccount.setCreateTime(DateUtils.getNowDate());
        return cmUserAccountMapper.insert(cmUserAccount);
    }

    /**
     * 修改业务员账号信息
     *
     * @param cmUserAccount 业务员账号信息
     * @return 结果
     */
    @Override
    public int updateCmUserAccount(CmUserAccount cmUserAccount) {
        return cmUserAccountMapper.updateById(cmUserAccount);
    }

    /**
     * 删除业务员账号信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmUserAccountByIds(Collection<Long> ids) {
        return cmUserAccountMapper.deleteBatchIds(ids);
    }

    /**
     * 删除业务员账号信息信息
     *
     * @param id 业务员账号信息ID
     * @return 结果
     */
    @Override
    public int deleteCmUserAccountById(Long id) {
        return cmUserAccountMapper.deleteById(id);
    }
}
